package zx.x.modules.sys.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import zx.x.common.utils.R;
import zx.x.modules.app.dao.OrderDao;
import zx.x.modules.app.dto.OrderDto;
import zx.x.modules.app.entity.*;
import zx.x.modules.app.service.*;
import zx.x.modules.app.utils.ExcelUtil;
import zx.x.modules.app.utils.PayUtil;
import zx.x.modules.app.utils.SnowFlake;
import zx.x.modules.app.utils.WxTkUtil;
import zx.x.modules.app.vo.OrderVo;
import zx.x.modules.common.service.CreativeGoodsService;
import zx.x.modules.sys.entity.SysExcelExport;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.util.*;


/**
 * @author x
 * @email x
 * @date 2022-10-13 09:55:21
 */
@RestController
@RequestMapping("/sys/order")
public class SysOrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    RoomAppointmentService roomAppointmentService;
    @Autowired
    OrderDao dao;
    @Autowired
    SysOrderTaskService sysOrderTaskService;
    @Autowired
    SysOrderLogService sysOrderLogService;
    @Autowired
    OrderPriceDetailsService orderPriceDetailsService; // 订单快照
    @Autowired
    ActivityRoomPriceService activityRoomPriceService;// 活动价格
    @Autowired
    MerchantRoomService merchantRoomService;
    @Autowired
    MerchantService merchantService;
    @Autowired
    MerchantWalletLogService merchantWalletLogService;
    @Autowired
    private CateringService cateringService;
    @Autowired
    private OrderJobLogService orderJobLogService;
    @Autowired
    private CreativeGoodsService creativeGoodsService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(OrderDto dto) throws Exception {
        Page page = PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<OrderVo> list = orderService.orderList(dto);
        return R.ok().put("page", list).put("pages", page.getPages()).put("total", page.getTotal());
    }


    /**
     * 信息
     */
    @RequestMapping("/queryByid")
    public R queryByid(Integer id) {
        OrderEntity order = orderService.getById(id);
        return R.ok().put("order", order);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(OrderEntity order) {
        orderService.updateById(order);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(Integer[] ids) {
        orderService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 发货
     */
    @RequestMapping("/fh")
    @Transactional
    public R fh(OrderEntity order) {
        // 修改订单状态
        OrderEntity o = new OrderEntity();
        o.setId(order.getId());
        o.setZt(9L);
        o.setWldh(order.getWldh());
        orderService.updateById(o);
        // 记录订单日志
        sysOrderLogService.save(new SysOrderLogEntity() {{
            setLogText("商家发货");
            setText("商家发货");
            setOId(o.getId());
            setZt(11);
        }});
        return R.ok();
    }


    /**
     * 退款修改
     */
    @Transactional
    @RequestMapping("/tkUpdate")
    public R tkUpdate(OrderDto order) {
        SysOrderLogEntity orderLog = new SysOrderLogEntity();
        OrderEntity obj = new OrderEntity();

        if (order.getTkZt() == 2) {
            WxTkUtil wxtk = new WxTkUtil();
            OrderEntity o = orderService.getById(order);
            BigDecimal price = new BigDecimal(String.valueOf(o.getMoney())).multiply(new BigDecimal(100));
            SnowFlake idWorker = new SnowFlake(0, 0);
            // 调用微信退款申请
            wxtk.refund(o.getNumber(), "TK" + String.valueOf(idWorker.nextId()), String.valueOf(price.intValue()), String.valueOf(price.intValue()), "REFUND_SOURCE_UNSETTLED_FUNDS");
            obj.setZt(11L);
            orderLog.setLogText("同意退款");
            orderLog.setZt(3);
        } else if (order.getTkZt() == 3) {
            orderLog.setLogText("拒绝退款");
            orderLog.setZt(12);
        }

        Date date = new Date();
        obj.setId(order.getId());
        obj.setTkZt(order.getTkZt());
        obj.setTkhzTime(date);
        orderService.updateById(obj);

        orderLog.setOId(order.getId());
        sysOrderLogService.save(orderLog);

        return R.ok();
    }

    /**
     * 收益图 全年总收益
     */
    @RequestMapping("/queryProfit")
    public R queryProfit(OrderDto dto) {
        ArrayList<Double> data = new ArrayList<Double>(Arrays.asList(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0));
        BigDecimal s = null;
        List<Map<Integer, BigDecimal>> mapList = orderService.queryProfit(dto);
        List<Map<Integer, BigDecimal>> mapListB = orderService.queryProfitB(dto);
        for (int i = 0; i < mapList.size(); i++) {
            for (int j = 0; j < mapListB.size(); j++) {
                System.out.println(mapList.get(i).get("yf"));
                System.out.println(mapListB.get(j).get("yf"));
                if (String.valueOf(mapList.get(i).get("yf")).equals(String.valueOf(mapListB.get(j).get("yf")))) {
                    // 匹配上的进入这里
                    s = mapList.get(i).get("sum").add(mapListB.get(j).get("sum"));
                    if ("01".equals(String.valueOf(mapList.get(i).get("yf")))) {
                        data.remove(0);
                        data.add(0,s.doubleValue());
                    }
                    if ("02".equals(String.valueOf(mapList.get(i).get("yf")))) {
                        data.remove(1);
                        data.add(1,s.doubleValue());
                    }
                    if ("03".equals(String.valueOf(mapList.get(i).get("yf")))) {
                        data.remove(2);
                        data.add(2,s.doubleValue());
                    }
                    if ("04".equals(String.valueOf(mapList.get(i).get("yf")))) {
                        data.remove(3);
                        data.add(3,s.doubleValue());
                    }
                    if ("05".equals(String.valueOf(mapList.get(i).get("yf")))) {
                        data.remove(4);
                        data.add(4,s.doubleValue());
                    }
                    if ("06".equals(String.valueOf(mapList.get(i).get("yf")))) {
                        data.remove(5);
                        data.add(5,s.doubleValue());
                    }
                    if ("07".equals(String.valueOf(mapList.get(i).get("yf")))) {
                        data.remove(6);
                        data.add(6,s.doubleValue());
                    }
                    if ("08".equals(String.valueOf(mapList.get(i).get("yf")))) {
                        data.remove(7);
                        data.add(7,s.doubleValue());
                    }
                    if ("09".equals(String.valueOf(mapList.get(i).get("yf")))) {
                        data.remove(8);
                        data.add(8,s.doubleValue());
                    }
                    if ("10".equals(String.valueOf(mapList.get(i).get("yf")))) {
                        data.remove(9);
                        data.add(9,s.doubleValue());                    }
                    if ("11".equals(String.valueOf(mapList.get(i).get("yf")))) {
                        data.remove(10);
                        data.add(10,s.doubleValue());
                    }
                    if ("12".equals(String.valueOf(mapList.get(i).get("yf")))) {
                        data.remove(11);
                        data.add(11,s.doubleValue());
                    }
                }
            }
        }

        for (int i = 0; i < mapList.size(); i++) {
            if ("01".equals(String.valueOf(mapList.get(i).get("yf")))&& data.get(0) == 0) {
                data.remove(0);
                data.add(0,mapList.get(i).get("sum").doubleValue());
            }
            if ("02".equals(String.valueOf(mapList.get(i).get("yf")))&& data.get(1) == 0) {
                data.remove(1);
                data.add(1,mapList.get(i).get("sum").doubleValue());
            }
            if ("03".equals(String.valueOf(mapList.get(i).get("yf")))&& data.get(2) == 0) {
                data.remove(2);
                data.add(2,mapList.get(i).get("sum").doubleValue());
            }
            if ("04".equals(String.valueOf(mapList.get(i).get("yf")))&& data.get(3) == 0) {
                data.remove(3);
                data.add(3,mapList.get(i).get("sum").doubleValue());
            }
            if ("05".equals(String.valueOf(mapList.get(i).get("yf")))&& data.get(4) == 0) {
                data.remove(4);
                data.add(4,mapList.get(i).get("sum").doubleValue());
            }
            if ("06".equals(String.valueOf(mapList.get(i).get("yf")))&& data.get(5) == 0) {
                data.remove(5);
                data.add(5,mapList.get(i).get("sum").doubleValue());
            }
            if ("07".equals(String.valueOf(mapList.get(i).get("yf")))&& data.get(6) == 0) {
                data.remove(6);
                data.add(6,mapList.get(i).get("sum").doubleValue());
            }
            if ("08".equals(String.valueOf(mapList.get(i).get("yf")))&& data.get(7) == 0) {
                data.remove(7);
                data.add(7,mapList.get(i).get("sum").doubleValue());
            }
            if ("09".equals(String.valueOf(mapList.get(i).get("yf")))&& data.get(8) == 0) {
                data.remove(8);
                data.add(8,mapList.get(i).get("sum").doubleValue());
            }
            if ("10".equals(String.valueOf(mapList.get(i).get("yf")))&& data.get(9) == 0) {
                data.remove(9);
                data.add(9,mapList.get(i).get("sum").doubleValue());
            }
            if ("11".equals(String.valueOf(mapList.get(i).get("yf")))&& data.get(10) == 0) {
                data.remove(10);
                data.add(10,mapList.get(i).get("sum").doubleValue());
            }
            if ("12".equals(String.valueOf(mapList.get(i).get("yf")))&& data.get(11) == 0) {
                data.remove(11);
                data.add(11,mapList.get(i).get("sum").doubleValue());
            }
        }

        for (int j = 0; j < mapListB.size(); j++) {
            if ("01".equals(String.valueOf(mapListB.get(j).get("yf"))) && data.get(0) == 0) {
                data.remove(0);
                data.add(0,mapListB.get(j).get("sum").doubleValue());
            }
            if ("02".equals(String.valueOf(mapListB.get(j).get("yf"))) && data.get(1) == 0) {
                data.remove(1);
                data.add(1,mapListB.get(j).get("sum").doubleValue());
            }
            if ("03".equals(String.valueOf(mapListB.get(j).get("yf"))) && data.get(2) == 0) {
                data.remove(2);
                data.add(2,mapListB.get(j).get("sum").doubleValue());
            }
            if ("04".equals(String.valueOf(mapListB.get(j).get("yf"))) && data.get(3) == 0) {
                data.remove(3);
                data.add(3,mapListB.get(j).get("sum").doubleValue());
            }
            if ("05".equals(String.valueOf(mapListB.get(j).get("yf")))&& data.get(4) == 0) {
                data.remove(4);
                data.add(4,mapListB.get(j).get("sum").doubleValue());
            }
            if ("06".equals(String.valueOf(mapListB.get(j).get("yf")))&& data.get(5) == 0) {
                data.remove(5);
                data.add(5,mapListB.get(j).get("sum").doubleValue());
            }
            if ("07".equals(String.valueOf(mapListB.get(j).get("yf")))&& data.get(6) == 0) {
                data.remove(6);
                data.add(6,mapListB.get(j).get("sum").doubleValue());
            }
            if ("08".equals(String.valueOf(mapListB.get(j).get("yf")))&& data.get(7) == 0) {
                data.remove(7);
                data.add(7,mapListB.get(j).get("sum").doubleValue());
            }
            if ("09".equals(String.valueOf(mapListB.get(j).get("yf")))&& data.get(8) == 0) {
                data.remove(8);
                data.add(8,mapListB.get(j).get("sum").doubleValue());
            }
            if ("10".equals(String.valueOf(mapListB.get(j).get("yf")))&& data.get(9) == 0) {
                data.remove(9);
                data.add(9,mapListB.get(j).get("sum").doubleValue());
            }
            if ("11".equals(String.valueOf(mapListB.get(j).get("yf"))) && data.get(10) == 0) {
                data.remove(10);
                data.add(10,mapListB.get(j).get("sum").doubleValue());
            }
            if ("12".equals(String.valueOf(mapListB.get(j).get("yf")))&& data.get(11) == 0) {
                data.remove(11);
                data.add(11,mapListB.get(j).get("sum").doubleValue());
            }
        }
        return R.ok().put("data", data);
    }

    /**
     * 收益图 全年总收益
     */
    @RequestMapping("/queryOrderCount")
    public R queryOrderCount(OrderDto dto) {
        ArrayList<Double> data = new ArrayList<Double>(Arrays.asList(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0));
        BigDecimal s = null;
        List<Map<Integer, Long>> mapList = orderService.queryOrderCount(dto);

        for (int i = 0; i < mapList.size(); i++) {
            if ("01".equals(String.valueOf(mapList.get(i).get("yf")))&& data.get(0) == 0) {
                data.remove(0);
                data.add(0,mapList.get(i).get("count").doubleValue());
            }
            if ("02".equals(String.valueOf(mapList.get(i).get("yf")))&& data.get(1) == 0) {
                data.remove(1);
                data.add(1,mapList.get(i).get("count").doubleValue());
            }
            if ("03".equals(String.valueOf(mapList.get(i).get("yf")))&& data.get(2) == 0) {
                data.remove(2);
                data.add(2,mapList.get(i).get("count").doubleValue());
            }
            if ("04".equals(String.valueOf(mapList.get(i).get("yf")))&& data.get(3) == 0) {
                data.remove(3);
                data.add(3,mapList.get(i).get("count").doubleValue());
            }
            if ("05".equals(String.valueOf(mapList.get(i).get("yf")))&& data.get(4) == 0) {
                data.remove(4);
                data.add(4,mapList.get(i).get("count").doubleValue());
            }
            if ("06".equals(String.valueOf(mapList.get(i).get("yf")))&& data.get(5) == 0) {
                data.remove(5);
                data.add(5,mapList.get(i).get("count").doubleValue());
            }
            if ("07".equals(String.valueOf(mapList.get(i).get("yf")))&& data.get(6) == 0) {
                data.remove(6);
                data.add(6,mapList.get(i).get("count").doubleValue());
            }
            if ("08".equals(String.valueOf(mapList.get(i).get("yf")))&& data.get(7) == 0) {
                data.remove(7);
                data.add(7,mapList.get(i).get("count").doubleValue());
            }
            if ("09".equals(String.valueOf(mapList.get(i).get("yf")))&& data.get(8) == 0) {
                data.remove(8);
                data.add(8,mapList.get(i).get("count").doubleValue());
            }
            if ("10".equals(String.valueOf(mapList.get(i).get("yf")))&& data.get(9) == 0) {
                data.remove(9);
                data.add(9,mapList.get(i).get("count").doubleValue());
            }
            if ("11".equals(String.valueOf(mapList.get(i).get("yf")))&& data.get(10) == 0) {
                data.remove(10);
                data.add(10,mapList.get(i).get("count").doubleValue());
            }
            if ("12".equals(String.valueOf(mapList.get(i).get("yf")))&& data.get(11) == 0) {
                data.remove(11);
                data.add(11,mapList.get(i).get("count").doubleValue());
            }
        }

        return R.ok().put("data", data);
    }

    /**
     * 收益图 全年总收益
     */
    @RequestMapping("/queryBzt")
    public R queryBzt(OrderDto dto) {
        List<Map<String,Integer>> list = new ArrayList<>();
        Integer wc  = orderService.queryWcOrder(dto.getYear());
        Integer tk  = orderService.queryTkOrder(dto);
        Integer spsl  = orderService.queryWcSl(dto);
        Integer zcsj  = orderService.queryZcSjCount(dto);
        Integer zcyh  = orderService.queryZcYh(dto);
        Integer plts  = orderService.queryPlTs(dto);
        Integer tsts  = orderService.queryTsTs(dto);
        Map a = new HashMap<>();
        a.put("name","完成订单数量");
        a.put("value",wc);

        Map b = new HashMap<>();
        b.put("name","退款订单数量");
        b.put("value",tk);

        Map c = new HashMap<>();
        c.put("name","文创商品上架数量");
        c.put("value",spsl);


        Map e = new HashMap<>();
        e.put("name","注册商户数量");
        e.put("value",zcsj);

        Map f = new HashMap<>();
        f.put("name","注册用户数量");
        f.put("value",zcyh);

        Map g = new HashMap<>();
        g.put("name","评论条数");
        g.put("value",plts);

        Map q = new HashMap<>();
        q.put("name","投诉条数");
        q.put("value",tsts);

        list.add(a);
        list.add(b);
        list.add(c);
        list.add(e);
        list.add(f);
        list.add(g);
        list.add(q);

        return R.ok().put("data", list);
    }

    /**
     * 下坠酒民餐收益图
     */
    @RequestMapping("/queryXzSy")
    public R queryXzSy(OrderDto dto) {
        List<Integer> moneyList = new ArrayList<>();
        List<String> nameList = new ArrayList<>();
        List<OrderVo> vo = orderService.queryXzSy(dto);
        for (OrderVo obj : vo) {
            moneyList.add(obj.getMoney().intValue());
            nameList.add(obj.getMName());
        }
        return R.ok().put("moneyList", moneyList).put("nameList",nameList);
    }

    /**
     * 下坠文创数量收益图
     */
    @RequestMapping("/queryXzWcSl")
    public R queryXzWcSl(OrderDto dto) {
        List<Integer> slList = new ArrayList<>();
        List<String> nameList = new ArrayList<>();
        List<OrderVo> vo = orderService.queryXzWcSl(dto);
        for (OrderVo obj : vo) {
            slList.add(obj.getSmts());
            nameList.add(obj.getSpName());
        }
        return R.ok().put("slList", slList).put("nameList",nameList);
    }

    /**
     * 收益图 票种 临时用
     */
    @RequestMapping("/queryPzSyt")
    public R queryPzSyt(OrderDto dto) {
        List<String[]> list = new ArrayList<>();
        OrderVo mp   = orderService.queryXzmpSl(dto);
        OrderVo dghf  = orderService.queryXzDghfcp(dto);
        OrderVo hyhf  = orderService.queryXzHyhfcp(dto);
        SysExcelExport e = new SysExcelExport();

        String[] a = new String[3];
        a[0] = "微山岛门票";
        if (mp.getMoney() == null){
            a[1] = "0";
            e.setWsdMpJe("0");
        }else {
            a[1] = mp.getMoney().toString();
            e.setWsdMpJe(mp.getMoney().toString());
        }
        if (mp.getSl() == null){
            a[2] = "0";
            e.setWsdMpSl("0");
        }else{
            a[2] = mp.getSl().toString();
            e.setWsdMpSl(mp.getSl().toString());
        }

        String[] b = new String[3];
        b[0] = "荷园画舫船票";
        if (hyhf.getMoney() == null){
            b[1] = "0";
            e.setHyhfJe("0");
        }else {
            b[1] = hyhf.getMoney().toString();
            e.setHyhfJe(hyhf.getMoney().toString());
        }
        if (hyhf.getSl() == null){
            b[2] = "0";
            e.setHyhfSl("0");
        }else{
            b[2] = hyhf.getSl().toString();
            e.setHyhfSl(hyhf.getSl().toString());
        }

        String[] c = new String[3];
        c[0] = "大官画舫船票";
        if (dghf.getMoney() == null){
            c[1] = "0";
            e.setDghfJe("0");
        }else {
            c[1] = dghf.getMoney().toString();
            e.setDghfJe(dghf.getMoney().toString());
        }
        if (dghf.getSl() == null){
            c[2] = "0";
            e.setDghfSl("0");
        }else{
            c[2] = dghf.getSl().toString();
            e.setDghfSl(dghf.getSl().toString());
        }
        e.setHjSl(String.valueOf(Integer.valueOf(e.getWsdMpSl()) + Integer.valueOf(e.getDghfSl()) + Integer.valueOf(e.getHyhfSl())));
        e.setHjJe(String.valueOf(Double.valueOf(e.getWsdMpJe()) + Double.valueOf(e.getDghfJe()) + Double.valueOf(e.getHyhfJe())));

        list.add(a);
        list.add(b);
        list.add(c);
        return R.ok().put("data", list).put("zSl",e.getHjSl()).put("zJe",e.getHjJe());
    }


    /*//普通导出
    public static void main(String[] args) throws FileNotFoundException {
        ArrayList<SysExcelExport> ls = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            SysExcelExport demo = new SysExcelExport();
            demo.setUsername("name"+i);
            ls.add(demo);
        }
        EasyExcel.write(new FileOutputStream("./demo.xlsx"),SysExcelExport.class).sheet(0).doWrite(ls);
    }*/


    @RequestMapping("/export")
    public void export(@RequestBody OrderDto dto,HttpServletResponse response) {
        ArrayList<SysExcelExport> ls = new ArrayList<>();

        OrderVo mp   = orderService.queryXzmpSl(dto);
        OrderVo dghf  = orderService.queryXzDghfcp(dto);
        OrderVo hyhf  = orderService.queryXzHyhfcp(dto);
        SysExcelExport e = new SysExcelExport();

        String[] a = new String[3];
        a[0] = "微山岛门票";
        if (mp.getMoney() == null){
            a[1] = "0";
            e.setWsdMpJe("0");
        }else {
            a[1] = mp.getMoney().toString();
            e.setWsdMpJe(mp.getMoney().toString());
        }
        if (mp.getSl() == null){
            a[2] = "0";
            e.setWsdMpSl("0");
        }else{
            a[2] = mp.getSl().toString();
            e.setWsdMpSl(mp.getSl().toString());
        }

        String[] b = new String[3];
        b[0] = "荷园画舫船票";
        if (hyhf.getMoney() == null){
            b[1] = "0";
            e.setHyhfJe("0");
        }else {
            b[1] = hyhf.getMoney().toString();
            e.setHyhfJe(hyhf.getMoney().toString());
        }
        if (hyhf.getSl() == null){
            b[2] = "0";
            e.setHyhfSl("0");
        }else{
            b[2] = hyhf.getSl().toString();
            e.setHyhfSl(hyhf.getSl().toString());
        }

        String[] c = new String[3];
        c[0] = "大官画舫船票";
        if (dghf.getMoney() == null){
            c[1] = "0";
            e.setDghfJe("0");
        }else {
            c[1] = dghf.getMoney().toString();
            e.setDghfJe(dghf.getMoney().toString());
        }
        if (dghf.getSl() == null){
            c[2] = "0";
            e.setDghfSl("0");
        }else{
            c[2] = dghf.getSl().toString();
            e.setDghfSl(dghf.getSl().toString());
        }
        e.setHjSl(String.valueOf(Integer.valueOf(e.getWsdMpSl()) + Integer.valueOf(e.getDghfSl()) + Integer.valueOf(e.getHyhfSl())));
        e.setHjJe(String.valueOf(Double.valueOf(e.getWsdMpJe()) + Double.valueOf(e.getDghfJe()) + Double.valueOf(e.getHyhfJe())));
        ls.add(e);

        String fName = "";
        if (dto.getKssj() !=null && !"".equals(dto.getKssj()) && dto.getJssj() != null && !"".equals(dto.getKssj())){
            fName =  "账目总览("+dto.getKssj()+"-"+dto.getJssj()+")";
        }else{
            fName =  "账目总览("+dto.getYear()+")";
        }
        ExcelUtil.exportExcel(ls, fName, SysExcelExport.class, response);
    }

}

